package cn.edu.scau.cmi.maintain.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Adultertype;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodbatch;
import cn.edu.scau.cmi.ema.domain.Foodtype;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.service.base.EntityFactoryService;
import cn.edu.scau.cmi.ema.util.ExcelUtil;
import cn.edu.scau.cmi.ema.util.SegmenterUtil;
import cn.edu.scau.cmi.ema.util.CmiSetUtil;
import cn.edu.scau.cmi.front.controller.FrontCmiEmaController;
import cn.edu.scau.cmi.front.dao.CmiEventDAO;
import cn.edu.scau.cmi.front.dao.CmiPictureDAO;
import cn.edu.scau.cmi.front.domain.FuzzyAddress;
import cn.edu.scau.cmi.front.domain.FuzzyFoodtype;
import cn.edu.scau.cmi.front.service.CmiFoodTypeIdService;
import cn.edu.scau.cmi.front.service.FuzzyAddressService;

/**
 * 没什么用的方法，待确认后逐步删除
 * 
 */

@Controller("foodMaintainController")

//@PropertySource(value = { "classpath:food2type.properties" })

public class MaintainFoodController extends FrontCmiEmaController {

	@RequestMapping(value = "/updateFoodtypeByFoodName/{foodName}/{foodtypeId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Object updateFoodtypeByFoodName(HttpServletRequest request, @PathVariable String foodName, @PathVariable Integer foodtypeId) {
		Food food = CmiSetUtil.getSoleInstance(foodDAO.findFoodByName(foodName));
//		如果foodtypeId是0，表示不修改食物类型，只修改食物的description.
		if(foodtypeId !=0) {
			food.setFoodtype(foodtypeDAO.findFoodtypeById(foodtypeId));
		}
		food.setDescription("食物类型已由梁方清人工审核，食物类型是：" + food.getFoodtype().getName());
		
		foodService.saveFood(food);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "SUCCESS");
		
		return jsonObject;
	}

	@RequestMapping(value = "/updateFoodtypeByFoodtypeName/{foodtypeName}/{foodId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Object updateFoodtypeByFoodtypeName(HttpServletRequest request, @PathVariable String foodtypeName, @PathVariable Integer foodId) {
		JSONObject result = new JSONObject();
		Food food = foodDAO.findFoodById(foodId);
		Set<Foodtype> foodtypes = foodtypeDAO.findFoodtypeByName(foodtypeName);
		if(foodtypes.size() ==0){
			result.put("result", "没有你刚才输入的这种类型");
			return result;
		}else{
			for(Foodtype ft : foodtypes){
//				迭代出来的食物类型的名称与输入的食物类型名称完全一致的时候，food的foodtype就是这个对象。
				if(ft.getName().equalsIgnoreCase(foodtypeName)){
					food.setFoodtype(ft);
					break;
				}
			}
		}


		food.setDescription("食物类型已由梁方清通过精确输入食物类型的名称人工审核，食物类型是：" + food.getFoodtype().getName());

		foodService.saveFood(food);

		result.put("result", "SUCCESS");

		return result;
	}






//	分页列出数据库中的food用于修改不合适的食物的名称和类别
	@RequestMapping("/listFoods4MaintainFoodtype")
	public ModelAndView listFoods4MaintainFoodtype(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {

		ModelAndView mav = new ModelAndView();
		
		pagination = foodPaginationService.updatePagination(request, pagination);
		
		Set<Food> foods = foodPaginationService.getPaginationFoods(pagination);

		mav.addObject("foods", foods);
		mav.addObject("pagination", pagination);
		mav.addObject("entityName", "Food");
		mav.setViewName("front/food/listFoods4MaintainFoodtype.jsp");
		return mav;
	}
}